package com.wgj.android.ui.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.jiaai.xui.utils.XAppUtils;
import com.jiaai.xui.utils.http.HttpCallBack;
import com.jiaai.xui.utils.http.ListHttpCallBack;
import com.jiaai.xui.utils.http.XHttp;
import com.jiaai.xui.view.widget.XAlertMsgDialog;
import com.jiaai.xui.view.widget.XToast;
import com.wgj.android.R;
import com.wgj.android.adapter.DescriptionAdapter;
import com.wgj.android.entity.NewHistory;
import com.wgj.android.entity.Version;
import com.wgj.android.entity.VersionVO;
import com.wgj.android.http.WGJAPI;
import com.wgj.android.utils.CacheUtils;
import com.wgj.android.utils.DownloadAppUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.jiaai.xui.XUI.getContext;
/**
 * ========================================
 * 作    者： 佳爱网络有限公司(wwww.jiaailove.com)
 * 版    本： 1.0
 * 创建日期：  2017/12/13
 * 描    述：  版本管理
 * 修订历吏：
 * =========================================
 */
public class VersionManageActivity extends AppCompatActivity {
    private  Version version;
    @BindView(R.id.versionText)
    TextView versionText;


    @BindView(R.id.updateMsg)
    TextView tvMsg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int flag= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        //设置当前窗体为全屏显示
        getWindow().setFlags(flag, flag);
        setContentView(R.layout.activity_new_function);
        ButterKnife.bind(this);
        version= CacheUtils.getVersion(this);
        versionText.setText(XAppUtils.getVersionName(this));
        XHttp.obtain().get(WGJAPI.BASEURL+"version?vNumber="+XAppUtils.getVersionCode(this),null, new ListHttpCallBack<VersionVO>() {

            @Override
            public void onSuccess(List<VersionVO> list) {
                 if(list!=null && list.size()>0){
                     tvMsg.setText(list.get(0).getContent());
                 }else {
                     tvMsg.setText(version.getChangelog());
                 }
            }

            @Override
            public void onFailed(int i, String s, boolean b) {
                tvMsg.setText(version.getChangelog());
            }
        });

    }


    @OnClick(R.id.checkVersion)
    public  void  checkVersion(View view){
        if(version.getVersion()>XAppUtils.getVersionCode(this)){
            new XAlertMsgDialog(this,version.getChangelog()).setTitle("发现新版本").setPositiveButton("下载").setListener(new XAlertMsgDialog.OnCloseListener() {
                @Override
                public void onClick(Dialog dialog, boolean b) {
                    if(b){
                        DownloadAppUtils.downloadForAutoInstall(VersionManageActivity.this,version.getInstallUrl(),"旺旺通app_v"+ version.getVersionShort(),"旺旺通发现新版本");
                    }else {
                        finish();
                    }
                }
            }).show();
        }else {
            XToast.success("当前是最新版");
        }
    }


    @OnClick(R.id.rl)
    public  void  showH(View view){
        Intent intent=new Intent(this,VersionHistoryActivity.class);
        startActivity(intent);
    }
    @OnClick(R.id.backBut)
    public void onBack(){
        finish();
    }
}
